package com.blyat.xsoft.auth.restful.entity;

import com.blyat.xsoft.kernel.restful.entity.XSoftEntity;
import com.google.common.collect.Sets;
import org.apache.ibatis.type.Alias;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * @Auther: syh
 * @Date: 2020/7/14
 * @Description:
 */
@Alias("sysClient")
public class Client extends XSoftEntity implements ClientDetails {

    private String clientId;
    private String resourceIds;
    private String clientSecret;
    private String scope;
    private String authorizedGrantTypes;

    private Collection<GrantedAuthority> authorities;
    private int accessTokenValiditySeconds;
    private int refreshTokenValiditySeconds;


    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    @Override
    public String getClientId() {
        return this.clientId;
    }

    public void setResourceIds(String resourceIds) {
        this.resourceIds = resourceIds;
    }

    @Override
    public Set<String> getResourceIds() {
        return this.resourceIds == null ? null: Sets.newHashSet(this.resourceIds.split(","));
    }

    @Override
    public boolean isSecretRequired() {
        return true;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    @Override
    public String getClientSecret() {
        return this.clientSecret;
    }

    @Override
    public boolean isScoped() {
        return true;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    @Override
    public Set<String> getScope() {
        return this.scope == null ? null: Sets.newHashSet(this.scope.split(","));
    }

    public void setAuthorizedGrantTypes(String authorizedGrantTypes) {
        this.authorizedGrantTypes = authorizedGrantTypes;
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        return this.authorizedGrantTypes == null ? null: Sets.newHashSet(this.authorizedGrantTypes.split(","));
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        return null;
    }

    public void setAuthorities(Collection<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    public void setAccessTokenValiditySeconds(int accessTokenValiditySeconds) {
        this.accessTokenValiditySeconds = accessTokenValiditySeconds;
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return this.accessTokenValiditySeconds;
    }

    public void setRefreshTokenValiditySeconds(int refreshTokenValiditySeconds) {
        this.refreshTokenValiditySeconds = refreshTokenValiditySeconds;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return this.refreshTokenValiditySeconds;
    }

    @Override
    public boolean isAutoApprove(String s) {
        return false;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return null;
    }
}
